package com.starblink.product.view

import androidx.fragment.app.FragmentActivity
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.basic.ext.load
import com.starblink.basic.util.CommUtils
import com.starblink.product.R
import com.starblink.product.databinding.ItemMountProductBinding
import com.starblink.rocketreserver.fragment.ProductF

class MountedProductHolder(
    binding: ItemMountProductBinding,
    val context: FragmentActivity,
    val needLeftPadding: Boolean = true,
    val showDialog: (() -> Unit)? = null,
    val dismissDialog: (() -> Unit)? = null
) :
    BaseBindingVH<ProductF, ItemMountProductBinding>(binding = binding) {

    override fun onBind(product: ProductF, position: Int) {
        binding.ivProduct1.load(product.mainImg, 112.dp, 112.dp)
        product.price?.let { binding.tvPrice.show(it, product.currency, product.currencySymbol) }

        binding.divide.visibleOrGone(needLeftPadding && position == 0) {}
        val disCountInfo = product.disCountInfo
        val cornerLabels = product.cornerLabels
        binding.tvTag.visibleOrGone(false) {}
        binding.tvTag.setBackgroundResource(R.drawable.bg_mount_new_tag)
        binding.tvPriceDiscount.visibleOrGone(false) {}
        if (disCountInfo != null) {
            binding.tvTag.visibleOrGone(disCountInfo.discountText?.isNotEmpty() == true) {
                binding.tvTag.text = "${disCountInfo.discountText}"
                binding.tvTag.setBackgroundResource(R.drawable.bg_mount_sale_tag)
                binding.tvTag.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray900))
            }

            binding.tvPriceDiscount.visibleOrGone(disCountInfo.discountText?.isNotEmpty() == true) {
                disCountInfo.underlinePrice?.let {
                    binding.tvPriceDiscount.show(
                        it,
                        product.currency,
                        product.currencySymbol
                    )
                }
            }
//        } else {
//            if (cornerLabels?.isNotEmpty() == true) {
//                val type = cornerLabels.first().type
//                binding.tvTag.setTextColor(CommUtils.getColor(com.starblink.basic.style.R.color.white))
//                binding.tvTag.visibleOrGone(type == 1 || type == 2 || type == 3 || type == 4) {
//                    setBackgroundResource(R.drawable.bg_mount_new_tag)
//                    text = cornerLabels.first().title
//                    if (type == 3) {
//                        setBackgroundResource(R.drawable.bg_mount_best_tag)
//                    }
//                }
//            }
        }

        binding.tvName.text = product.title

        itemView.setOnClickListener {
            showDialog?.invoke()
            ProductMountedDialog(
                context, product, pageTrackTag = this.hashCode().toString()
            ) {
                dismissDialog?.invoke()
            }.show(context.supportFragmentManager, "mounted")
        }
    }
}